package com.tarena.lbs.activity.service.impl;

import com.tarena.lbs.activity.pojo.param.ActivityAddParam;
import com.tarena.lbs.activity.pojo.query.ActivityQuery;
import com.tarena.lbs.activity.pojo.vo.ActivityVO;
import com.tarena.lbs.activity.dao.mapper.ActivityMapper;
import com.tarena.lbs.activity.pojo.po.ActivityPO;
import com.tarena.lbs.activity.service.ActivityService;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public PageResult<ActivityVO> pageList(ActivityQuery query) throws BusinessException {
        Integer businessId = query.getBusinessId();
        int pageNo = query.getPageNo() != null ? query.getPageNo() : 1;
        int pageSize = query.getPageSize() != null ? query.getPageSize() : 10;
        int offset = (pageNo - 1) * pageSize;

        List<ActivityVO> vos = activityMapper.selectPage(businessId, offset, pageSize);
        Long total = activityMapper.count(businessId);

        PageResult<ActivityVO> pageResult = new PageResult<>();
        pageResult.setPageNo(pageNo);
        pageResult.setPageSize(pageSize);
        pageResult.setTotal(total == null ? 0L : total);
        pageResult.setObjects(vos);
        return pageResult;
    }

    @Override
    public void activitySave(ActivityAddParam param) throws BusinessException {
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        // 校验必填
        if (param.getActivityName() == null) {
            throw new BusinessException("-1", "活动名称不能为空");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start;
        Date end;
        try {
            start = sdf.parse(param.getStartDate());
            end = sdf.parse(param.getEndDate());
        } catch (ParseException e) {
            throw new BusinessException("-1", "日期格式错误，需 yyyy-MM-dd HH:mm:ss");
        }

        ActivityPO po = new ActivityPO();
        po.setActivityName(param.getActivityName());
        po.setStartDate(start);
        po.setEndDate(end);
        po.setDescribes(param.getDescribes());
        po.setImgIds(param.getImgIds());
        po.setShopIds(param.getShopIds());
        po.setTargetCustomer(param.getTargetCustomer());
        po.setActivityTarget(param.getActivityTarget());
        po.setDeliveryChannel(param.getDeliveryChannel());
        // reward_coupon_id 为 varchar
        po.setRewardCouponId(String.valueOf(param.getRewardCouponId()));
        po.setRewardType(param.getRewardType());
        po.setShareProfile(param.getShareProfile());
        po.setShareUrl(param.getShareUrl());
        po.setStatus(0);
        po.setBusinessId(param.getBusinessId());
        Date now = new Date();
        po.setCreateAt(now);
        po.setUpdateAt(now);
        po.setEnableStatus(1);
        po.setActivityType(1);
        po.setChannelType(param.getDeliveryChannel());
        po.setMarketingType(param.getMarketingType());
        po.setActivityJson(param.getActivityJson());

        activityMapper.save(po);
    }

    @Override
    public ActivityVO detail(Integer id) throws BusinessException {
        ActivityPO po = activityMapper.getDetailById(id);
        if (po == null) {
            throw new BusinessException("-1", "活动不存在");
        }
        ActivityVO vo = new ActivityVO();
        BeanUtils.copyProperties(po, vo);
        // PO中rewardCouponId为String，VO为Integer，做类型转换
        try {
            if (po.getRewardCouponId() != null) {
                vo.setRewardCouponId(Integer.valueOf(po.getRewardCouponId()));
            }
        } catch (NumberFormatException ignore) {
            vo.setRewardCouponId(null);
        }
        return vo;
    }
}
